//
//  ProfileViewController.m
//  SnapCAD
//
//  Created by Developer on 2025/1/27.
//

#import "ProfileViewController.h"
#import "PrivacyPolicyViewController.h"
#import "TermsOfServiceViewController.h"
#import "Masonry.h"

@interface ProfileViewController ()

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation ProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupConstraints];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.97 alpha:1.0];
    
    [self setupHeader];
    [self setupScrollView];
    [self setupContent];
}

- (void)setupHeader {
    // 头部视图
    self.headerView = [[UIView alloc] init];
    self.headerView.backgroundColor = [UIColor whiteColor];
    
    // 添加阴影
    self.headerView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.headerView.layer.shadowOffset = CGSizeMake(0, 2);
    self.headerView.layer.shadowOpacity = 0.1;
    self.headerView.layer.shadowRadius = 4;
    
    self.headerView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.headerView];
    
    // 返回按钮
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backButton setImage:[UIImage systemImageNamed:@"chevron.left"] forState:UIControlStateNormal];
    self.backButton.tintColor = [UIColor colorWithRed:0.40 green:0.49 blue:0.93 alpha:1.0];
    [self.backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    self.backButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.headerView addSubview:self.backButton];
    
    // 标题标签
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"Profile";
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.headerView addSubview:self.titleLabel];
}

- (void)setupScrollView {
    // 滚动视图
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.scrollView];
    
    // 内容视图
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor clearColor];
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addSubview:self.contentView];
}

- (void)setupContent {
    // 用户信息卡片
    UIView *userInfoCard = [self createUserInfoCard];
    [self.contentView addSubview:userInfoCard];
    
    // 设置选项卡片
    UIView *settingsCard = [self createSettingsCard];
    [self.contentView addSubview:settingsCard];
    
    // 约束设置
    [userInfoCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(20);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.height.equalTo(@120);
    }];
    
    [settingsCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userInfoCard.mas_bottom).offset(20);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
        make.bottom.equalTo(self.contentView).offset(-20);
    }];
}

- (UIView *)createUserInfoCard {
    UIView *cardView = [[UIView alloc] init];
    cardView.backgroundColor = [UIColor whiteColor];
    cardView.layer.cornerRadius = 12;
    cardView.layer.shadowColor = [UIColor blackColor].CGColor;
    cardView.layer.shadowOffset = CGSizeMake(0, 2);
    cardView.layer.shadowOpacity = 0.1;
    cardView.layer.shadowRadius = 8;
    
    // 头像
    UIImageView *avatarImageView = [[UIImageView alloc] init];
    avatarImageView.backgroundColor = [UIColor colorWithRed:0.40 green:0.49 blue:0.93 alpha:1.0];
    avatarImageView.layer.cornerRadius = 30;
    avatarImageView.clipsToBounds = YES;
    
    // 添加用户图标
    UIImageView *userIcon = [[UIImageView alloc] init];
    userIcon.image = [UIImage systemImageNamed:@"person.fill"];
    userIcon.tintColor = [UIColor whiteColor];
    userIcon.contentMode = UIViewContentModeScaleAspectFit;
    [avatarImageView addSubview:userIcon];
    
    // 用户名
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"SnapCAD User";
    nameLabel.font = [UIFont boldSystemFontOfSize:18];
    nameLabel.textColor = [UIColor blackColor];
    
    // 用户描述
    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.text = @"Welcome to SnapCAD";
    descLabel.font = [UIFont systemFontOfSize:14];
    descLabel.textColor = [UIColor grayColor];
    
    [cardView addSubview:avatarImageView];
    [cardView addSubview:nameLabel];
    [cardView addSubview:descLabel];
    
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cardView).offset(20);
        make.centerY.equalTo(cardView);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    [userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(avatarImageView);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarImageView.mas_right).offset(16);
        make.right.equalTo(cardView).offset(-20);
        make.top.equalTo(avatarImageView.mas_top).offset(8);
    }];
    
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.right.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(4);
    }];
    
    return cardView;
}

- (UIView *)createSettingsCard {
    UIView *cardView = [[UIView alloc] init];
    cardView.backgroundColor = [UIColor whiteColor];
    cardView.layer.cornerRadius = 12;
    cardView.layer.shadowColor = [UIColor blackColor].CGColor;
    cardView.layer.shadowOffset = CGSizeMake(0, 2);
    cardView.layer.shadowOpacity = 0.1;
    cardView.layer.shadowRadius = 8;
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"Legal";
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = [UIColor blackColor];
    [cardView addSubview:titleLabel];
    
    // 隐私协议选项
    UIView *privacyOption = [self createOptionView:@"Privacy Policy" 
                                              icon:@"lock.shield" 
                                            action:@selector(privacyPolicyTapped)];
    [cardView addSubview:privacyOption];
    
    // 分割线
    UIView *separatorLine = [[UIView alloc] init];
    separatorLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    [cardView addSubview:separatorLine];
    
    // 服务协议选项
    UIView *termsOption = [self createOptionView:@"Terms of Service" 
                                            icon:@"doc.text" 
                                          action:@selector(termsOfServiceTapped)];
    [cardView addSubview:termsOption];
    
    // 约束设置
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cardView).offset(20);
        make.left.equalTo(cardView).offset(20);
        make.right.equalTo(cardView).offset(-20);
    }];
    
    [privacyOption mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(16);
        make.left.right.equalTo(cardView);
        make.height.equalTo(@56);
    }];
    
    [separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(privacyOption.mas_bottom);
        make.left.equalTo(cardView).offset(20);
        make.right.equalTo(cardView).offset(-20);
        make.height.equalTo(@1);
    }];
    
    [termsOption mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(separatorLine.mas_bottom);
        make.left.right.equalTo(cardView);
        make.height.equalTo(@56);
        make.bottom.equalTo(cardView).offset(-20);
    }];
    
    return cardView;
}

- (UIView *)createOptionView:(NSString *)title icon:(NSString *)iconName action:(SEL)action {
    // 使用 UIButton 作为容器，这样可以直接处理点击事件
    UIButton *optionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    optionButton.backgroundColor = [UIColor clearColor];
    [optionButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    // 图标
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.image = [UIImage systemImageNamed:iconName];
    iconView.tintColor = [UIColor colorWithRed:0.40 green:0.49 blue:0.93 alpha:1.0];
    iconView.contentMode = UIViewContentModeScaleAspectFit;
    iconView.userInteractionEnabled = NO; // 确保不会拦截点击事件
    [optionButton addSubview:iconView];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.userInteractionEnabled = NO; // 确保不会拦截点击事件
    [optionButton addSubview:titleLabel];
    
    // 箭头
    UIImageView *arrowView = [[UIImageView alloc] init];
    arrowView.image = [UIImage systemImageNamed:@"chevron.right"];
    arrowView.tintColor = [UIColor lightGrayColor];
    arrowView.contentMode = UIViewContentModeScaleAspectFit;
    arrowView.userInteractionEnabled = NO; // 确保不会拦截点击事件
    [optionButton addSubview:arrowView];
    
    // 约束设置
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(optionButton).offset(20);
        make.centerY.equalTo(optionButton);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(16);
        make.centerY.equalTo(optionButton);
        make.right.equalTo(arrowView.mas_left).offset(-16);
    }];
    
    [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(optionButton).offset(-20);
        make.centerY.equalTo(optionButton);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    
    // 添加点击效果
    [optionButton addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
    [optionButton addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside | UIControlEventTouchCancel];
    
    return optionButton;
}

- (void)buttonTouchDown:(UIButton *)button {
    [UIView animateWithDuration:0.1 animations:^{
        button.transform = CGAffineTransformMakeScale(0.95, 0.95);
        button.alpha = 0.8;
    }];
}

- (void)buttonTouchUp:(UIButton *)button {
    [UIView animateWithDuration:0.1 animations:^{
        button.transform = CGAffineTransformIdentity;
        button.alpha = 1.0;
    }];
}

// 删除原来的手势识别器相关方法
// - (void)addTapEffectToView:(UIView *)view
// - (void)handleLongPress:(UILongPressGestureRecognizer *)gesture

- (NSInteger)getTagForAction:(SEL)action {
    if (action == @selector(privacyPolicyTapped)) {
        return 1001;
    } else if (action == @selector(termsOfServiceTapped)) {
        return 1002;
    }
    return 0;
}

- (void)handleOptionTap:(UILongPressGestureRecognizer *)gesture {
    UIView *view = gesture.view;
    
    if (gesture.state == UIGestureRecognizerStateBegan) {
        [UIView animateWithDuration:0.1 animations:^{
            view.transform = CGAffineTransformMakeScale(0.95, 0.95);
            view.alpha = 0.8;
        }];
    } else if (gesture.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.1 animations:^{
            view.transform = CGAffineTransformIdentity;
            view.alpha = 1.0;
        } completion:^(BOOL finished) {
            // 在动画完成后执行相应的动作
            if (view.tag == 1001) {
                [self privacyPolicyTapped];
            } else if (view.tag == 1002) {
                [self termsOfServiceTapped];
            }
        }];
    } else if (gesture.state == UIGestureRecognizerStateCancelled) {
        [UIView animateWithDuration:0.1 animations:^{
            view.transform = CGAffineTransformIdentity;
            view.alpha = 1.0;
        }];
    }
}

// 删除原来的 addTapEffectToView 和 handleLongPress 方法，因为我们已经合并到 handleOptionTap 中了

- (void)setupConstraints {
    // 头部视图约束
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@88);
    }];
    
    // 返回按钮约束
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView).offset(16);
        make.bottom.equalTo(self.headerView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    // 标题标签约束
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headerView);
        make.centerY.equalTo(self.backButton);
    }];
    
    // 滚动视图约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    // 内容视图约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
}

#pragma mark - Button Actions

- (void)backButtonTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)privacyPolicyTapped {
    PrivacyPolicyViewController *privacyVC = [[PrivacyPolicyViewController alloc] init];
    [self.navigationController pushViewController:privacyVC animated:YES];
}

- (void)termsOfServiceTapped {
    TermsOfServiceViewController *termsVC = [[TermsOfServiceViewController alloc] init];
    [self.navigationController pushViewController:termsVC animated:YES];
}

@end